﻿using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using Utils;
using DataBaseConnect.App.MessageApp;
using Entity.Dto;
using Entity;

// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace CoreMVC.Jurisdiction.Manage.Controllers
{
    public class MessageController : BaseController
    {
        #region 构造实例化

        public IMessageAppService _messageAppService;

        public MessageController(IMessageAppService messageAppService)
        {
            _messageAppService = messageAppService;
        }

        #endregion

        #region 视图区

        // GET: /<controller>/
        public IActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 消息发送页面
        /// </summary>
        /// <returns></returns>
        public IActionResult SendMessage()
        {
            return View();
        }

        /// <summary>
        /// 消息接收页面
        /// </summary>
        /// <returns></returns>
        public IActionResult ReceiveMessgae()
        {
            ViewData["msg"] = ReceiveMag();
            return View();
        }

        /// <summary>
        /// MQ 连接
        /// </summary>
        public void Conntent(string message)
        {
            RabbitMqStatu.SendMsg(message, 1);
        }

        public ReceiveStatus ReceiveMag()
        {
            return RabbitMqStatu.ReceiveMag("");
        }

        #endregion

        #region 功能实现区

        /// <summary>
        /// 获取消息(分页)
        /// </summary>
        /// <param name="MenuName">菜单名</param>
        /// <param name="page">当前页码</param>
        /// <param name="limit">每页显示条数</param>
        /// <returns></returns>
        public JsonResult GetMessageList(int IsSee, int page, int limit)
        {
            ReceiveStatus<MessageDto> receiveStatus = new ReceiveStatus<MessageDto>();
            int rowCount = 0;
            List<MessageDto> list = new List<MessageDto>();
            try
            {
                list = _messageAppService.GetMessageList(IsSee,page,limit,out rowCount);
                receiveStatus.msg = "成功";
                receiveStatus.data = list;
                receiveStatus.count = rowCount;
            }
            catch (Exception ex)
            {
                receiveStatus.msg = "失败：" + ex.Message;
                receiveStatus.code = statuCode.Error;
            }
            return Json(receiveStatus);
        }
        /// <summary>
        /// 获取消息条数
        /// </summary>
        /// <returns></returns>
        public JsonResult GetUnMessageList()
        {
            ReceiveStatus<MessageDto> receiveStatus = new ReceiveStatus<MessageDto>();

            List<MessageDto> list = new List<MessageDto>();
            try
            {
                list = _messageAppService.GetMessageCount(0);
                receiveStatus.msg = "成功";
                receiveStatus.data = list;
                receiveStatus.count = list.Count;
            }
            catch (Exception ex)
            {
                receiveStatus.msg = "失败：" + ex.Message;
                receiveStatus.code = statuCode.Error;
            }
            return Json(receiveStatus);
        }

        /// <summary>
        /// 更新为已读（EF 指定字段更新）
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public JsonResult UpdateMessage(List<Message> Id)
        {
            ReceiveStatus receiveStatus = new ReceiveStatus();
            foreach (var item in Id)
            {
                receiveStatus = _messageAppService.UpdateMessage(item.Id);
            }
            return Json(receiveStatus);
        }

        #endregion
    }
}
